package com.fudan.backend.dao;

import com.fudan.backend.handler.IdTypeHandler;
import com.fudan.backend.handler.ListTypeHandler;
import com.fudan.backend.pojo.Activity;
import com.fudan.backend.pojo.Commodity;
import com.fudan.backend.pojo.util.CommodityActivityInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CommodityDao {
    @Results(id = "commodityResultMap", value = {
            @Result(column = "commodity_id", property = "commodityId", typeHandler = IdTypeHandler.class),
            @Result(column = "store_id", property = "storeId", typeHandler = IdTypeHandler.class),
            @Result(column = "category_id", property = "categoryId", typeHandler = IdTypeHandler.class),
            @Result(column = "pictures", property = "pictures", typeHandler = ListTypeHandler.class)
    })
    @Select("select * from t_commodity where commodity_id = #{commodityId, typeHandler=com.fudan.backend.handler.IdTypeHandler}")
    Commodity selectCommodityByCommodityId(String commodityId);

    @ResultMap("commodityResultMap")
    @Select("select * from t_commodity where store_id = #{storeId, typeHandler=com.fudan.backend.handler.IdTypeHandler} and status = 1")
    List<Commodity> selectAllCommodityByStoreId(String storeId);

    @ResultMap("commodityResultMap")
    @Select("select * from t_commodity where store_id = #{storeId, typeHandler=com.fudan.backend.handler.IdTypeHandler} and " +
            "category_id = #{categoryId, typeHandler=com.fudan.backend.handler.IdTypeHandler} and status = 1")
    List<Commodity> selectAllCommodityByStoreIdAndCategoryId(String storeId, String categoryId);

    @ResultMap("commodityResultMap")
    @Select("select * from t_commodity")
    List<Commodity> selectAllCommodity();

    @ResultMap("commodityResultMap")
    @Select("<script>" +
            "select * from t_commodity " +
            "where commodity_id in " +
            "<foreach item='id' collection='list' open='(' close=')' separator=','>" +
            "#{id, typeHandler=com.fudan.backend.handler.IdTypeHandler}" +
            "</foreach>" +
            "</script>")
    List<Commodity> selectCommodityListByCommodityIdList(List<String> list);
    @Results(id = "commodityInfoMap", value = {
            @Result(column = "commodity_id", property = "commodity", typeHandler = IdTypeHandler.class, javaType = Commodity.class,
            one = @One(select = "com.fudan.backend.dao.CommodityDao.selectCommodityByCommodityId")),
            @Result(column = "activity_id", property = "activity",typeHandler = IdTypeHandler.class, javaType = Activity.class,
            one = @One(select = "com.fudan.backend.dao.ActivityDao.selectActivityByActivityId"))
    })
    @Select("<script>" +
            "select commodity_id, activity_id from t_commodity as tc join t_store as ts " +
            "on tc.store_id = ts.store_id " +
            "where commodity_id in " +
            "<foreach item='id' collection='list' open='(' close=')' separator=','>" +
            "#{id, typeHandler=com.fudan.backend.handler.IdTypeHandler}" +
            "</foreach>" +
            "</script>")
    List<CommodityActivityInfo> selectCommodityInfosByCommodityIdList(List<String> list);

    @Insert("insert into t_commodity values " +
            "(#{commodityId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "#{storeId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "#{commodityName}, #{categoryId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "#{price}, #{stocks}, #{pictures, typeHandler=com.fudan.backend.handler.ListTypeHandler}, " +
            "#{description}, #{salesVolume}, #{launchDate}, #{status})")
    int insertCommodity(Commodity commodity);

    @Update("update t_commodity set " +
            "store_id = #{storeId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "commodity_name = #{commodityName}, category_id = #{categoryId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "price = #{price}, stocks = #{stocks}, pictures = #{pictures, typeHandler=com.fudan.backend.handler.ListTypeHandler}, " +
            "description = #{description}, sales_volume = #{salesVolume}, launch_date = #{launchDate}, status = #{status} " +
            "where commodity_id = #{commodityId, typeHandler=com.fudan.backend.handler.IdTypeHandler}")
    int updateCommodity(Commodity commodity);

    /**
     * 设置商品状态
     */
    @Update("update t_commodity set " +
            "status = #{status} " +
            "where commodity_id = #{commodityId, typeHandler=com.fudan.backend.handler.IdTypeHandler}")
    Integer setCommodityStatus(String commodityId, Integer status);

    /**
     * 变更与给定商店相关的所有商品状态
     */
    @Update("update t_commodity set " +
            "status = #{status} " +
            "where store_id = #{storeId, typeHandler=com.fudan.backend.handler.IdTypeHandler}")
    Integer setCommodityStatusByStoreId(String storeId, Integer status);

    /**
     * 销量增加num
     */
    @Update("update t_commodity set " +
            "stocks = stocks + #{num} " +
            "where commodity_id = #{commodityId}")
    int addCommodityStock(String commodityId, int num);
    @Update("update t_commodity set " +
            "sales_volume = sales_volume + #{num} " +
            "where commodity_id = #{commodityId}")
    int addCommoditySaleVolume(String commodityId, int num);
}
